@extends('front.layouts.app')
@section('content')
	<section id="cart_items">
		<div class="container">
				<nav aria-label="breadcrumb" role="navigation">
				  <ol class="breadcrumb">
				    <li class="breadcrumb-item"><a href="/">首页</a></li>
				    <li class="breadcrumb-item active" aria-current="page">购物车</li>
				  </ol>
				</nav>
			@if(Auth::check())
				@include('front.checkout.login-checkout')
			@else
				@include('front.checkout.not-login-checkout')
			@endif
			<!-- end Form -->
			<div class="review-payment">
				<h2>Review & Payment</h2>
			</div>

			<div class="table-responsive cart_info">
				<table class="table table-condensed">
					<thead>
						<tr class="cart_menu">
							<td class="image">Item</td>
							<td class="description"></td>
							<td class="price">Price</td>
							<td class="quantity">Quantity</td>
							<td class="total">Total</td>
							<td></td>
						</tr>
					</thead>
					<tbody>
						@foreach($carts as $cart)

						<tr>
							<td class="cart_product"  > 
								<a href="{{route('details',$cart->id)}}"><img src="{{asset('productpics').'/'.$cart->pimg}}" width="90" height="90" style="text-align: center;" alt=""></a>
							</td>
							<td class="cart_description">
								<h4><a href="">{{$cart->name}}</a></h4>
								<p>商品编号:{{$cart->id}}</p> 
							</td>
							<td class="cart_price" >
								<span style="float:left">￥</span><p id="price-{{$cart->id}}"> {{$cart->price }}</p>
							</td>
							<td class="cart_quantity">
								<div class="cart_quantity_button">
									<a class="cart_quantity_up" href="javascript:;"  onclick="update_count({{$cart->id}},'{{$cart->rowId}}','0','',{{$cart->stock}})"> + </a>
									<input class="cart_quantity_input" id="quantity-{{$cart->id}}"  type="text" name="quantity" value="{{$cart->qty}}" onchange="update_count({{$cart->id}},'{{$cart->rowId}}',2,1,{{$cart->stock}})" autocomplete="off" size="2">
									<a class="cart_quantity_down" href="javascript:;" onclick="update_count({{$cart->id}},'{{$cart->rowId}}','1','',{{$cart->stock}})"> - </a>
								</div>
							</td>
							<td class="cart_total">
								 <p class="cart_total_price" id="tl-{{$cart->id}}">￥ {{$cart->price * $cart->qty}}</p>
							</td>
							<td class="cart_delete">
								<a class="cart_quantity_delete" href="{{ route('cart-del',$cart->rowId) }}"><i class="fa fa-times"></i></a>
							</td>
						</tr>
			 @endforeach 
						<tr> 
							<td colspan="4"></td>
							<td colspan="2">
								<table class="table table-condensed total-result">
									<tr>
										<td>Cart Sub Total</td>
										<td id="subtotal">￥{{$subtotal}}</td>
									</tr>
									<tr>
										<td>Exo Tax</td>
										<td id="tax">-</td>
									</tr>
									<tr class="shipping-cost">
										<td>Shipping Cost</td>
										<td>Free</td>										
									</tr>
									<tr>
										<td>Total</td>
										<td><span id="subtotals">￥{{$subtotal}} </span></td>
									</tr>
								</table>
							</td>
						</tr>
					</tbody>
				</table>

			</div>
			
			<div class="payment-options">
<textarea name="message"  placeholder="留言"></textarea>
					{{-- <span>
						<label><input type="checkbox"> Direct Bank Transfer</label>
					</span> --}}
					<span>
						<label><input type="radio" name="alipay"> 支付宝支付</label>
					</span>
					<span>
						<label><input type="radio" checked name="wxpay"> 微信支付</label>
					</span>
					<div>
						<a class="btn btn-primary" href="{{ route('order.pay') }}" style="float:right" >Pay Out</a>
					</div>
				</div>
		</div>

	</section> <!--/#cart_items-->

	
    <script>
   	function update_count(id,rowId,type,qty=null,stocks)
		{
			var stock = $("#stock").text().substr(3);
			var qty = parseInt($("#quantity-"+id).val());

			// alert(stock);

			if( qty > stock){

				alert('不能大于库存');
				$("#quantity-"+id).val(stock);
				return false;

			}

			$.post('/shop-crat/'+rowId+'/update',{'_token':"{{csrf_token()}}",'qty':$("#quantity-"+id).val(),'oldqty':qty,'type':type},function(rsp){

				if(rsp.status === 'plus'){

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#quantity-"+id).val('('+qty+')'); 
					$("#quantity-"+id).val(qty+=1); 
					$("#tax").text('￥ '+rsp.tax);
					$('#tl-'+id).text('￥ '+$("#quantity-"+id).val() * $('#price-'+id).text());
					// $("#subtotal").text('￥ '+$())

				}else if(rsp.status === 'ajan'){

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#quantity-"+id).val('('+qty+')'); 
					$("#quantity-"+id).val(qty-=1); 
					$("#tax").text('￥ '+rsp.tax);
					$('#tl-'+id).text('￥ '+$("#quantity-"+id).val() * $('#price-'+id).text());
				}else if(rsp.status === 'maxqtx'){
					alert('超过库存(不要作恶)');
				}
				else{

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#tax").text('￥ '+rsp.tax);
					$("#quantity-"+id).val(qty); 
					$('#tl-'+id).text('￥ '+ $("#quantity-"+id).val() * $('#price-'+id).text());


				}

				if(rsp.status === 'null'){
					window.location.href='';
				}
			})
		}
   </script>
   @endsection